home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / samples / midp_lin / midplx.c < prev   
Encoding:
C/C++ Source or Header  |  1997-01-26  |  6.0 KB  |  290 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <ctype.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #include "midas.h"
  8.  
  9. #include <curses.h>
  10.  
  11.  
  12.  
  13. #define MIDPVERSION 0.1.3
  14. #define MIDPVERSTR "0.1.3"
  15.  
  16. gmpModule       *module;                /* current playing module */
  17. gmpInformation  *info;                  /* current module playing info */
  18. int             callMP;
  19.  
  20.  
  21. char            *title =
  22. "\nMIDAS Module Player for Linux, version " MIDPVERSTR "\n"
  23. "Copyright 1996-1997 Jarno Paananen\n";
  24.  
  25.  
  26. char            *usage =
  27. "Usage:\tMIDP\t[options] <filename> [options]\n"\
  28. "Options:\n"\
  29. "\t-?, -h\t This information\n"\
  30. "\t-mx\tMixing rate x Hz\n"\
  31. "\t-ox\tOutput mode (8=8-bit, 1=16-bit, s=stereo, m=mono\n\n"\
  32. "Keys while playing:\n"\
  33. "\tESC\tExit\n"\
  34. "\t+/-\tMaster volume control\n"\
  35. "\t./,\tJump forward/backward in the song\n"\
  36. "\n";
  37.  
  38.  
  39. static int mode, rate, end;
  40.  
  41.  
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     int                error;
  46.     static fileHandle  f;
  47.     static uchar       buf[48];
  48.     int                i, j, k;
  49.     gmpInstrument      *inst;
  50.     char               *fileName = NULL;
  51.     char               *name;
  52.  
  53.     int                key, oldRow = 0;
  54.  
  55.     int                masterVolume = 64;
  56.  
  57.  
  58.     setbuf(stdout, NULL);
  59.     setbuf(stdin, NULL);
  60.  
  61.     puts(title);
  62.  
  63.     midasSetDefaults();
  64.  
  65.     if ( argc < 2 )
  66.     {
  67.         puts(usage);
  68.         exit(EXIT_SUCCESS);
  69.     }
  70.  
  71.     midasSetDefaults();
  72.  
  73. /* These settings control the default output mode, useful for GUS people at least */
  74. //    midasOutputMode |= sd8bit;
  75. //    midasOutputMode |= sdMono;
  76. //    midasOutputMode &= ~sdStereo;
  77.  
  78.     for ( i = 1 ; i < argc; i++ )
  79.     {
  80.         if ( argv[i][0] == '-' )
  81.     {
  82.         switch ( argv[i][1] )
  83.         {
  84.             case 'm':
  85.             sscanf(&argv[i][2], "%d", &midasMixRate );
  86.             break;
  87.  
  88.             case 'o':
  89.             for ( j = 2; j < 4; j++ )
  90.             {
  91.                 switch ( argv[i][j] )
  92.             {
  93.                 case '1':
  94.                     midasOutputMode &= ~sd8bit;
  95.                 midasOutputMode |= sd16bit;
  96.                     break;
  97.                 case '8':
  98.                     midasOutputMode &= ~sd16bit;
  99.                 midasOutputMode |= sd8bit;
  100.                     break;
  101.                 case 's':
  102.                     midasOutputMode &= ~sdMono;
  103.                 midasOutputMode |= sdStereo;
  104.                     break;
  105.                 case 'm':
  106.                     midasOutputMode &= ~sdStereo;
  107.                 midasOutputMode |= sdMono;
  108.                     break;
  109.                         }
  110.             }
  111.             break;
  112.  
  113.             default:
  114.             fileName = argv[i];  // :)
  115.         }
  116.     }
  117.     else
  118.         fileName = argv[i];
  119.  
  120.     }
  121.  
  122.     if ( fileName == NULL )
  123.     {
  124.         puts("No filename given!\n");
  125.     return 1;
  126.     }
  127.  
  128.     midasInit();
  129.  
  130.     /* Read first 48 bytes of module: */
  131.     if ( (error = fileOpen(fileName, fileOpenRead, &f)) != OK )
  132.         midasError(error);
  133.     if ( (error = fileRead(f, buf, 48)) != OK )
  134.         midasError(error);
  135.     if ( (error = fileClose(f)) != OK )
  136.         midasError(error);
  137.  
  138.     if ( mMemEqual(buf, "Extended Module:", 16) )
  139.     {
  140.         puts("Loading Fasttracker 2 module");
  141.         if ( (error = gmpLoadXM(fileName, 1, NULL, &module)) != OK )
  142.             midasError(error);
  143.     }
  144.     else
  145.     {
  146.         if ( mMemEqual(buf+44, "SCRM", 4) )
  147.         {
  148.             puts("Loading Screamtracker 3 module");
  149.             if ( (error = gmpLoadS3M(fileName, 1, NULL, &module)) != OK )
  150.                 midasError(error);
  151.         }
  152.         else
  153.     {
  154.              puts("Loading Protracker module");
  155.                  if((error = gmpLoadMOD(fileName, 1, NULL, &module)) != OK )
  156.                      midasError(error);
  157.     }
  158.     }
  159.  
  160.     printf("Module: %s\n", module->name);
  161.  
  162.     k = ( module->numInsts + 1) / 2;
  163.  
  164.     for ( i = 0; i < k; i++ )
  165.     {
  166.         inst = module->instruments[i];
  167.     name = inst->name;
  168.     while ( *name != 0 )
  169.     {
  170.         if ( *name < 32 )
  171.             *name = 32;
  172.         name++;
  173.     }
  174.  
  175.         printf( "\n%02x: %-35s", i + 1, inst->name );
  176.         if ( i + k < (int)module->numInsts )
  177.         {
  178.         inst = module->instruments[i+k];
  179.         name = inst->name;
  180.         while ( *name != 0 )
  181.         {
  182.             if ( *name < 32 )
  183.                 *name = 32;
  184.         name++;
  185.         }
  186.             printf( "%02x: %s", i + k + 1, inst->name );
  187.         }      
  188.     }
  189.  
  190.     printf("\n\n");
  191.     midasPlayModule(module, 0);
  192.  
  193.     midasSD->GetMixRate(&rate);
  194.     printf("Playing at %d Hz", rate);
  195.     
  196.     midasSD->GetMode(&mode);
  197.     if ( mode & sd8bit )
  198.         printf(" 8-bit");
  199.     else
  200.         printf(" 16-bit");
  201.  
  202.     if ( mode & sdMono )
  203.         printf(" mono");
  204.     else
  205.         printf(" stereo");
  206.  
  207.     end = 0;
  208.  
  209.     puts("\n");
  210.  
  211.     /* Initialize ncurses */
  212.  
  213.     atexit((void*)endwin);
  214.     initscr();
  215.     cbreak();
  216.     nodelay(stdscr, TRUE);
  217.     noecho();
  218.  
  219.  
  220.     /* Start playing thread */
  221.  
  222.     StartPlayThread(100);
  223.  
  224.     /* The Main Loop */
  225.     while( !end )
  226.     {
  227.         if ( (error = gmpGetInformation(midasPlayHandle, &info)) != OK )
  228.         midasError(error);
  229.  
  230.     if ( info->row != (unsigned)oldRow )
  231.     {
  232.         oldRow = info->row;
  233.         printf("Position: %02x Pattern: %02x Row: %02x\r", info->position, info->pattern,
  234.            info->row);
  235.  
  236.         fflush(stdout);
  237.     }
  238.  
  239.  
  240.     key = getch();
  241.  
  242.  
  243.         switch ( key )
  244.         {
  245.             case 27:
  246.                 end = 1;
  247.                 break;
  248.  
  249.             case '-':
  250.                 if ( masterVolume > 0 )
  251.                     masterVolume--;
  252.                 if ( (error = midasSD->SetMasterVolume(masterVolume))
  253.                     != OK )
  254.                     midasError(error);
  255.                 break;
  256.  
  257.             case '+':
  258.                 if ( masterVolume < 64 )
  259.                     masterVolume++;
  260.                 if ( (error = midasSD->SetMasterVolume(masterVolume))
  261.                     != OK )
  262.                     midasError(error);
  263.                 break;
  264.  
  265.              case '.':         /* Right arrow */
  266.             gmpSetPosition( midasPlayHandle, info->position + 1 );
  267.         break;
  268.  
  269.         case ',':         /* Left arrow */
  270.             gmpSetPosition( midasPlayHandle, info->position - 1 );
  271.         break;
  272.         }
  273.     usleep(50000);
  274.     }
  275.  
  276.     /* Wait for the playing thread to return */
  277.     StopPlayThread();
  278.  
  279.  
  280.     /* And off we go... */
  281.     printf("\n");
  282.     midasStopModule(module);
  283.     if ( (error = gmpFreeModule(module)) != OK )
  284.         midasError(error);
  285.     midasClose();
  286.     errPrintList();
  287.  
  288.     return 0;
  289. }
  290.